package com.changgou.web.order.controller;

import com.changgou.entity.Result;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.pay.feign.PayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @author zero
 * @date 2020/7/13
 */

@Controller
@RequestMapping("/wxpay")
public class PayController {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private PayFeign payFeign;


    @GetMapping
    public String wxPay(String orderId , Model model){

        //根据订单id查询订单是否存在
        Order order = orderFeign.findById(orderId).getData();
        if (order == null){
            return "fail";
        }
        //判断订单的状态是否未完成
        if (!"0".equals(order.getPayStatus())){
            return "fail";
        }
        //调用微信统一下单的接口
        Result<Map> payResult = payFeign.nativePay(orderId, order.getPayMoney());
        if (payResult.getData() == null){
            return "fail";
        }
        //封装数据结果
        Map payMap = payResult.getData();
        payMap.put("orderId",orderId);
        payMap.put("payMoney",order.getPayMoney());

        model.addAllAttributes(payMap);

        return "wxpay";
    }


    @RequestMapping("/toPaySuccess")
    public String toPaySuccess(Integer payMoney , Model model){

        model.addAttribute("payMoney",payMoney);

        return "paysuccess";

    }

}
